# 中译英的翻译小工具
import urllib.request as ur
import urllib.parse as up
import json

while True:
    keyword = input('请输入要翻译的中文：')
    data = {'kw': keyword}  # 请求数据按照network中的表单来写
    data_url = up.urlencode(data)  # url编码
    try:
        request = ur.Request(
            url='https://fanyi.baidu.com/sug',
            data=data_url.encode('utf-8')  # 再次编码
        )
        response = ur.urlopen(request).read()
        ret = json.loads(response)
        # print(ret)
        english = ret['data'][0]['v']
        print(english)
    except:
        print('未能找到对应英语翻译！')
